from selenium.webdriver.common.by import By
from page_objects.base_page import BasePage
from utils.log_utils import logger


class CartsMoisturizePage(BasePage):

    __ALL_BTN_NUMS = (By.CLASS_NAME, "btn.btn-primary")
    __ROW_BTN_NUMS = (By.XPATH, "//div[2]/div")
    __CARD_ITEMS = (By.ID, 'cart')

    """商品列表页面：获取添加按钮数"""
    def get_btns(self):
        row_btn_numbers = self.do_finds(self.__ROW_BTN_NUMS)
        all_btn_numbers = self.do_finds(self.__ALL_BTN_NUMS)
        row_num, res_num =  len(row_btn_numbers), len(all_btn_numbers)
        logger.info(f"商品列表页面，每个table行添加按钮数为：{row_num},所有添加按钮数为：{res_num}")

        return [row_num, res_num]

    """商品列表页面：点击添加按钮，添加商品到购物车"""
    def get_cartitems(self):
        logger.info("商品列表页面：点击添加按钮，添加商品到购物车")
        for element in self.do_finds(self.__ALL_BTN_NUMS):
            element.click()
        element = self.wait_element_until_visible(self.__CARD_ITEMS)
        # 获取购物车文本信息
        items_num = int(element.text.split(" ")[0])
        logger.info(f"已添加到购物车的商品是：{items_num}")

        # 返回购物车文本
        return [items_num, self.get_btns()[1]]